{extend name="public/base"/}
{block name="style"}
<style>
    .layui-table th{text-align:center;}
    .layui-table td{text-align:center;}
    .text-left{text-align: left !important;}
</style>
{/block}
{block name="content"}
<div class="data-list" data-url="{:url('index')}">
    <form class="layui-form inline-form">
        <div class="pull-left">
            <div class="layui-inline">
                <input type="text" name="sn" placeholder="采购单号/追踪号" autofocus class="layui-input input-order-sn">
            </div>
            <div class="layui-inline">
                <input type="text" placeholder="收货日期" name="recv_date" class="layui-input laydate-range">
            </div>
            <div class="layui-inline">
                <select name="warehouse_id" lay-filter="data-list" lay-search>
                    <option value="">调入仓库</option>
                    {foreach $warehouse as $w}
                    <option value="{$w.warehouse_id}">{$w.name}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <select name="producer_id" lay-filter="data-list" lay-search>
                    <option value="">供应商</option>
                    {foreach $producer as $p}
                    <option value="{$p.producer_id}">{$p.name}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <select name="order_status" lay-filter="data-list" lay-search>
                    <option value="">采购单状态</option>
                    {foreach $order_status as $k=>$v}
                    <option value="{$k}">{$v}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <select name="track_status" lay-filter="data-list" lay-search>
                    <option value="">物流状态</option>
                    {foreach $track_status as $k=>$v}
                    <option value="{$k}">{$v}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm layui-btn-normal search"><i class="layui-icon layui-icon-search"></i></button>
            </div>
        </div>
    </form>
    <div class="layui-row form-lists-button">
        <div class="layui-inline layui-btn-group">
            <a href="{:url('add')}" class="ajax-form layui-btn layui-btn-normal layui-btn-sm" title="添加采购追踪号" width-form="550px" height-form="70%"><i class="layui-icon layui-icon-add-circle"></i>添加追踪号</a>
            <a href="{:url('/file/download', ['file' => '/attachment/template/purchase-track-template.xlsx', 'v'=>1.0])}" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-download-circle"></i>下载模板</a>
            <a id="import-track" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-upload-drag"></i>导入追踪号</a>
            <a id="batch-sync-info" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-refresh"></i>拉取物流信息</a>
        </div>
    </div>
    <div class="data">
        <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
    layui.use(['layer', 'jquery', 'upload'], function()
    {
        var layer = layui.layer
            , $ = layui.jquery
            , upload = layui.upload;

        // 导入追踪号
        upload.render({
            elem: '#import-track'
            , url: '{:url("file/upload",["type"=>"document"])}'
            , accept: 'file' //普通文件
            , exts: 'xlsx|xls|csv' // 指定类型
            , field: "file"
            , before: function (obj) {
                layer.load(0, {shade: false});
            }
            , done: function (response, index, upload) {
                if (response.code != 1) {
                    layer.closeAll('loading');
                    var icon = response.code == 1 ? 6 : 5;
                    layer.msg(response.msg, {icon: icon, time: 1500});
                    return;
                }
                $.post("{:url('upload')}", {"path": response.data.src}).done(function(response)
                {
                    layer.closeAll('loading');
                    var icon = 5, time = 5500;
                    if (response.code == 1) {
                        icon = 6;
                        time = 1500;
                    }
                    layer.msg(response.msg.replace(/\n/g, "<br/>"), {icon, time}, function()
                    {
                        $(".data").closest('.data-list').getList();
                    });
                });
            }
            , error: function (index, upload) {
                layer.msg('上传失败');
            }
        });

        $("#batch-sync-info").click(function()
        {
            var ids = getIds('.track-id');
            if(ids.length < 1)
            {
                layer.msg("请选择要同步的采购单", {icon: 5, time: 1500});
                return false;
            }
            layer.load(0, {shade: false});
            $.get("{:url('track')}", {ids: ids.join(',')}).done(function(response)
            {
                layer.closeAll('loading');
                var icon = 5, time = 1500;
                if(response.code == 1)
                {
                    $(".data").closest('.data-list').getList();
                    icon = 6;
                    time = 3000;
                }
                layer.msg(response.msg, {icon: icon, time: time});
            });
        });
    });
</script>
{/block}